package com.xueyi.workflow.convert;

import com.xueyi.common.core.web.entity.model.BaseConverter;
import com.xueyi.workflow.domain.query.WfProcessTaskQuery;
import com.xueyi.workflow.domain.dto.WfProcessTaskDto;
import com.xueyi.workflow.domain.entity.WfProcessTask;
import com.github.pagehelper.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class WfProcessTaskConverter implements BaseConverter<WfProcessTaskQuery, WfProcessTaskDto, WfProcessTask> {
    @Override
    public WfProcessTaskDto mapperDto(WfProcessTask po) {
        if (po == null) return null;
        WfProcessTaskDto dto = new WfProcessTaskDto();
        BeanUtils.copyProperties(po, dto);
        return dto;
    }
    @Override
    public List<WfProcessTaskDto> mapperDto(Collection<WfProcessTask> poList) {
        if (poList == null) return null;
        return poList.stream().map(this::mapperDto).collect(Collectors.toList());
    }
    @Override
    public Page<WfProcessTaskDto> mapperPageDto(Collection<WfProcessTask> poList) {
        if (poList == null) return null;
        if (poList instanceof Page<WfProcessTask> pagePoList) {
            Page<WfProcessTaskDto> pageDtoList = new Page<>();
            pageDtoList.setPageNum(pagePoList.getPageNum());
            pageDtoList.setPageSize(pagePoList.getPageSize());
            pageDtoList.setTotal(pagePoList.getTotal());
            pageDtoList.setPages(pagePoList.getPages());
            pageDtoList.setCount(pagePoList.isCount());
            pageDtoList.setReasonable(pagePoList.getReasonable());
            pageDtoList.setPageSizeZero(pagePoList.getPageSizeZero());
            pageDtoList.setCountColumn(pagePoList.getCountColumn());
            pageDtoList.setOrderBy(pagePoList.getOrderBy());
            pageDtoList.setOrderByOnly(pagePoList.isOrderByOnly());
            
            List<WfProcessTaskDto> dtoList = poList.stream()
                .map(this::mapperDto)
                .collect(Collectors.toList());
            pageDtoList.addAll(dtoList);
            
            return pageDtoList;
        }
        return null;
    }
    @Override
    public WfProcessTask mapperPo(WfProcessTaskDto dto) {
        if (dto == null) return null;
        WfProcessTask po = new WfProcessTask();
        BeanUtils.copyProperties(dto, po);
        return po;
    }
    @Override
    public List<WfProcessTask> mapperPo(Collection<WfProcessTaskDto> dtoList) {
        if (dtoList == null) return null;
        return dtoList.stream().map(this::mapperPo).collect(Collectors.toList());
    }
    @Override
    public Page<WfProcessTask> mapperPagePo(Collection<WfProcessTaskDto> dtoList) {
        if (dtoList == null) return null;
        if (dtoList instanceof Page<WfProcessTaskDto> pageDtoList) {
            Page<WfProcessTask> pagePoList = new Page<>();
            pagePoList.setPageNum(pageDtoList.getPageNum());
            pagePoList.setPageSize(pageDtoList.getPageSize());
            pagePoList.setTotal(pageDtoList.getTotal());
            pagePoList.setPages(pageDtoList.getPages());
            pagePoList.setCount(pageDtoList.isCount());
            pagePoList.setReasonable(pageDtoList.getReasonable());
            pagePoList.setPageSizeZero(pageDtoList.getPageSizeZero());
            pagePoList.setCountColumn(pageDtoList.getCountColumn());
            pagePoList.setOrderBy(pageDtoList.getOrderBy());
            pagePoList.setOrderByOnly(pageDtoList.isOrderByOnly());
            
            List<WfProcessTask> poList = dtoList.stream()
                .map(this::mapperPo)
                .collect(Collectors.toList());
            pagePoList.addAll(poList);
            
            return pagePoList;
        }
        return null;
    }
} 